package com.mesSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.Equipment;
import com.mesSystem.entity.Workstation;
import com.mesSystem.service.impl.EquipmentService;
import com.mesSystem.service.impl.WorkstationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.util.calendar.CalendarDate;

import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;
@RestController
@Slf4j
@ResponseBody
@RequestMapping("workstation")
public class WorkstationController {
    @Autowired
    WorkstationService workstationService;

    @PostMapping("increase")
    public Result increase(@RequestBody Workstation workstation) {
        //传入创建时间
        java.util.Date currentDate = new java.util.Date();
        Date sqlDate = new Date(currentDate.getTime());
        workstation.setIncreaseDate(sqlDate);

        workstationService.save(workstation);
        log.info("添加工位:{}",workstation);
        return Result.ok("创建成功");
    }

    @DeleteMapping("delete")
    public Result delete(Long workstationId) {
        workstationService.removeById(workstationId);
        return Result.ok("删除成功");
    }

    /**
     * 分页查询信息
     */
    @GetMapping("query")
    public Result query(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        // 创建分页器对象
        Page<Workstation> Page = new Page<>(page, size);

        // 调用分页查询方法，将分页器传递给service
        workstationService.page(Page, new QueryWrapper<Workstation>().orderByAsc("workstation_id"));

        // 从分页器对象中获取分页后的数据
        List<Workstation> workstations = Page.getRecords();

        log.info("被查询信息为：{}", workstations);

        // 返回分页结果
        return Result.ok(workstations);
    }

    /**
     * 更改
     */
    @PostMapping("update")
    public Result update(@RequestBody Workstation workstation) {
        if (workstation.getWorkstationId() == null) {
            return Result.fail("没有传达工位id！");
        }
        workstationService.updateById(workstation);
        log.info("update:{}",workstation);
        return Result.ok("更新成功");
    }
}
